#include ".\automanager.h"
#include "process.h"
#include "windows.h"
#include <string>
#include <iostream>
using namespace std;


extern CRuleSet g_RULESET;
bool AM_active;

#define SERVER_PORT 5001
#define AM_WAKEUP_RATE 50 


CAutoManager::CAutoManager(void)
{

}

CAutoManager::~CAutoManager(void)
{
}
UINT CAutoManager::AM_start()
{
	unsigned int threadID;
	AM_active=1;
	(HANDLE)::_beginthreadex(0,0,AM_RUN,(void*)this,0,&threadID);
	return 0;
}
UINT __stdcall CAutoManager::AM_STOP(void * para)
{
	AM_active=0;
	return 0;
}
UINT __stdcall CAutoManager::AM_RUN(void * para)
{
	while(AM_active)
	{	
		if(!g_RULESET.IsEmpty())
		{
			CAutoManager::AM_sense();
			CAutoManager::AM_act();
			Sleep(AM_WAKEUP_RATE);
		}
	}
	return 0;
}
UINT CAutoManager::AM_sense()
{
	g_RULESET.refresh_rule_staus();
	return 0;
}
unsigned int  CAutoManager::AM_act(void)
{
	map<string,CRule>::iterator currule;	
	currule=g_RULESET.GethighestRule(); 
	if(currule==NULL)
	{
		return -1;
	}
	else 
	{
		if(currule->second.active==true)
		{
			currule->second.exe_eca_rule();
		}
		
		return 0;
	}
}
